// pages/mine/backstage/budget/budget.js 收支明细

const App = getApp()

let { AJAX, timeTranfer, imgUrl} = require("../../../../utils/util.js")
Page({
 
   /**
    * 页面的初始数据
    */
   data: {  
      navH: 0,
      list: [  
         { 
            id: 1, 
            title:'下单返佣'
         },  
         {
            id: 2,
            title: '提现'
         },
         {
            id: 3,
            title: '合伙人提成'
         }
      ],
      
      // 展开折叠
      selectedFlag: false,       
      years:'',
      months:'',
      info: [],
      page: 1,
      total: 0, 
      income:'', 
      withdrawal:'',
      is_nodata: true,
      floorstatus: false,
      text: '加载更多',
      showThis: false,
      showIcon: false,
      isLoading: false,
     imgUrl: imgUrl
     
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function (options) {
      let vm = this
      this.setData({
         navH: App.globalData.navHeight
      })
      if (!wx.getStorageSync("ACCESSTOKEN")) {
         App.userInfoReadyCallback = res => {
            wx.setStorageSync('ACCESSTOKEN', res);
            vm.getPayment()
         }
         return
      }
      vm.getPayment()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function () {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function () {
   //    this.setData({
   //       page: 1
   //    })
   //   this.getPayment()
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function () {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function () {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function () {

      this.setData({
         page: 1
      })
      this.getPayment()
      // 显示顶部刷新图标
      wx.showNavigationBarLoading();
   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function () {
      let vm = this
     
      if (vm.data.total > vm.data.info.length ) {
         vm.setData({
            page: vm.data.page + 1,
            showIcon: true,
            showThis: true
         })
         vm.getPayment(true)
      } else {
         if (vm.data.info.length!=''){
            this.setData({
               text: '我是有底线的',
               showIcon: false,
               showThis: true
            })
         }
      }
   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function () {

   },
   // 获取滚动条当前位置
   onPageScroll: function (e) {
      console.log(e)
      if (e.scrollTop > 200) {
         this.setData({
            floorstatus: true
         });
      } else {
         this.setData({
            floorstatus: false
         });
      }
   },
   goTop: function (e) {  // 一键回到顶部
      if (wx.pageScrollTo) {
         wx.pageScrollTo({
            scrollTop: 0
         })
      } else {
         wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
         })
      }
   },
   /**
    * 收支明细
    * finance/payment
    */
   getPayment(str){
      let vm = this
      AJAX("finance/payment", "GET", {
         page: vm.data.page
      }).then(res => {
         // 隐藏导航栏加载框
         wx.hideNavigationBarLoading();
         // 停止下拉动作
         wx.stopPullDownRefresh();
         if (res.have_data == true) {
            let _data = []
            if (str) {
               _data = vm.data.info
            }
            if (res.data.item==''){
                 this.setData({
                    is_nodata: false
                 }) 
            } else{
               res.data.item.forEach((item, index) => {
                  _data.push(item)
               })

            }
           
          

            vm.setData({
               total: res.header["X-Pagination-Total-Count"],
               info: _data,
               income: res.data.income,
               withdrawal: res.data.withdrawal,
            })

         } else {
            this.setData({
               is_nodata: false
            })
         }

      })
   },
   // 选择时间
   getDateTime(e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      let str = e.detail.value.toString()

      let year = str.substring(0, str.indexOf("-"));
      let month = str.substring(str.indexOf("-") + 1);    
      this.setData({
         date: e.detail.value,
         years: year,
         months: month
      })
   },
   // 详情
   toInfo(e){
      //  console.log(e.currentTarget.dataset.infoid)
      let _id = e.currentTarget.dataset.infoid
      wx.navigateTo({
         url: '/pages/mine/backstage/bill/bill?id=' + _id,
      })
   },

   // 展开折叠选择  
   changeToggle: function (e) {
      var index = e.currentTarget.dataset.index;
      if (this.data.selectedFlag) {
         this.data.selectedFlag = false;
      } else {
         this.data.selectedFlag = true;
      }
      this.setData({
         selectedFlag: this.data.selectedFlag
      })
   },
   navBack() {
      wx.navigateBack()
   }
})